package com.haiheng.voiceandbook.api

import android.util.Log
import com.haiheng.voiceandbook.Constants.Companion.BASE_URL
import com.haiheng.voiceandbook.interceptor.HeadInterceptor
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Path
import retrofit2.http.Streaming
import retrofit2.http.Url
import java.util.concurrent.TimeUnit


interface DownLoadService {
    companion object Factory {

//        val BASE_URL = "https://test.seaeverit.com"
        val downLoadService = create()

        fun create(): DownLoadService {
            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(HeadInterceptor())
//                .addInterceptor(HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> //访问网络请求，和服务端响应请求时。将数据拦截并输出
//                    Log.e("KotlinApiService", "RetrofitHelper-Http: $message")
//                }).setLevel(HttpLoggingInterceptor.Level.BODY)) //Log等级
                .connectTimeout(600, TimeUnit.SECONDS).build()
            val retrofit = Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .baseUrl(BASE_URL)
                .build()
            return retrofit.create(DownLoadService::class.java)
        }
    }


    // 在 downLoadUrl 方法中添加 Range 请求头
    @Streaming
    @GET
    suspend fun downLoadUrl(@Url path: String?, @Header("Range") range: String?): ResponseBody

    @Streaming
    @GET
    suspend fun downLoadUrl(@Url path: String?): ResponseBody
}